package com.jiangxiaofei.citycloud.activity.village;

import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.RequiresApi;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.ScrollView;

import com.jiangxiaofei.citycloud.R;
import com.jiangxiaofei.citycloud.adapter.VillageMenuAdapter;
import com.jiangxiaofei.citycloud.adapter.VillageNewsAdapter;
import com.jiangxiaofei.citycloud.base.BaseActivity;
import com.jiangxiaofei.citycloud.bean.VillageMenuBean;
import com.jiangxiaofei.citycloud.bean.VillageNewsBean;
import com.jiangxiaofei.citycloud.constants.StringConstants;
import com.jiangxiaofei.citycloud.mvp.VillageConstant;
import com.jiangxiaofei.citycloud.mvp.VillagePersenter;
import com.jiangxiaofei.citycloud.param.VillageListParam;
import com.jiangxiaofei.citycloud.util.BannerImageLoder;
import com.jiangxiaofei.citycloud.view.FullyGridLayoutManager;
import com.jiangxiaofei.citycloud.view.ProgressDialogHelper;
import com.jiangxiaofei.citycloud.view.SlidingListView;
import com.scwang.smartrefresh.header.MaterialHeader;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.constant.SpinnerStyle;
import com.scwang.smartrefresh.layout.footer.BallPulseFooter;
import com.scwang.smartrefresh.layout.listener.OnLoadmoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.youth.banner.Banner;

import java.util.ArrayList;
import java.util.List;

import butterknife.ButterKnife;
import butterknife.InjectView;
import butterknife.OnClick;

/**
 * Created by JiangXFei on 2017/8/16.
 * 美丽乡村 主界面
 */

@RequiresApi(api = Build.VERSION_CODES.M)
public class VillageAcitivity extends BaseActivity implements VillageConstant.VillagemMenuView, VillageConstant.VillageListView {
    @InjectView(R.id.village_banner)
    Banner villageBanner;
    @InjectView(R.id.village_gv_menu)
    RecyclerView villageGvMenu;
    @InjectView(R.id.village_lv)
    SlidingListView villageLv;
    RefreshLayout refreshLayout;
    @InjectView(R.id.village_img_back_top)
    ImageView villageImgBackTop;
    @InjectView(R.id.village_scroll_main)
    ScrollView villageScrollMain;

    private List<Integer> imgList = new ArrayList<>();
    private List<VillageNewsBean> newsList = new ArrayList<>();
    private List<VillageMenuBean> menuList;
    private VillageConstant.VillageMenuPersenter mVillageMenuPersenter;
    private VillageNewsAdapter newsAdapter;
    private VillageMenuAdapter menuAdapter;
    private VillageConstant.VillageListPersenter mVillageListPersenter;
    private int page = 1;

    @RequiresApi(api = Build.VERSION_CODES.M)
    @Override
    protected void initView(Bundle savedInstanceState) {
        mContext = this;
        mActivity = this;
        setContentView(R.layout.activity_village_layout);

        ButterKnife.inject(this);
        showLoadView();
        mVillageMenuPersenter = new VillagePersenter(mContext, (VillageConstant.VillagemMenuView) mActivity);
        mVillageMenuPersenter.getVillageMenu();
        mVillageListPersenter = new VillagePersenter(mContext, (VillageConstant.VillageListView) mActivity);
        mVillageListPersenter.getVillageList();
        initTitle();
        initData();
//        villageScrollMain.setOnScrollChangeListener(this);
    }


    private void initData() {
        refreshLayout = (RefreshLayout) findViewById(R.id.refreshLayout);
        imgList.clear();
        imgList.add(R.drawable.slide1);
        imgList.add(R.drawable.slide2);
        imgList.add(R.drawable.slide3);
        villageBanner.setImageLoader(new BannerImageLoder());
        villageBanner.setImages(imgList);
        villageBanner.start();
        menuList = new ArrayList<>();
        villageGvMenu.setLayoutManager(new FullyGridLayoutManager(mContext, 3));
        villageGvMenu.setHasFixedSize(true);
        menuAdapter = new VillageMenuAdapter(mContext, menuList);
        villageGvMenu.setAdapter(menuAdapter);
        newsAdapter = new VillageNewsAdapter(mContext, newsList);
        villageLv.setAdapter(newsAdapter);
        refreshLayout.setRefreshHeader(new MaterialHeader(this).setShowBezierWave(true));
        refreshLayout.setRefreshFooter(new BallPulseFooter(this).setSpinnerStyle(SpinnerStyle.Scale));

        refreshLayout.setOnLoadmoreListener(new OnLoadmoreListener() {
            @Override
            public void onLoadmore(RefreshLayout refreshlayout) {
                refreshlayout.finishLoadmore(2000);
                page++;
                mVillageListPersenter.getVillageList();
            }
        });
        refreshLayout.setOnRefreshListener(new OnRefreshListener() {

            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                refreshlayout.finishRefresh(2000);
                page = 1;
                newsList.clear();
                mVillageListPersenter.getVillageList();
            }
        });
    }

    @Override
    protected void setListener() {
        mRightThree.setOnClickListener(this);
    }

    @Override
    protected void processLogic(Bundle savedInstanceState) {
        setCTitle("美丽乡村");
        setLeftOneBg(R.drawable.ic_back);
        setRightThreeBg(R.drawable.ic_publish);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_rightthree:
                //菜单按钮的点击事件
                Intent intent = new Intent(mContext, VillageInfoPublishActivity.class);
                intent.putExtra(StringConstants.PUBLISHTYPE, VillageInfoPublishActivity.PUBLISH);
                mContext.startActivity(intent);
                break;
            default:
                break;
        }
    }

    @OnClick({R.id.village_img_back_top})
    public void OnClick(View v) {
        switch (v.getId()) {
            case R.id.village_img_back_top:
                villageScrollMain.scrollTo(0,0);
                break;
            default:
                break;
        }
    }

    @Override
    public void setData(Object o) {
        List list = (List) o;
        if (list.get(0) instanceof VillageNewsBean) {
            for (int i = 0; i < list.size(); i++) {
                newsList.add((VillageNewsBean) list.get(i));
            }
            newsAdapter.notifyDataSetChanged();
        } else if (list.get(0) instanceof VillageMenuBean) {
            for (int i = 0; i < list.size(); i++) {
                menuList.add((VillageMenuBean) list.get(i));
            }
            menuAdapter.notifyDataSetChanged();
        }
    }


    @Override
    public void showLoading() {
        ProgressDialogHelper.show(mContext);
    }

    @Override
    public void hideLoading() {
        ProgressDialogHelper.dismissDialog();
    }

    @Override
    public void message(String str) {

    }


    @Override
    public void getVillageMenu() {

    }

    @Override
    public VillageListParam getVillageList() {
        return new VillageListParam(String.valueOf(page), "0");
    }



}
